import Vue from 'vue';
import iView from 'iview';
import Util from '../libs/util';
import VueRouter from 'vue-router';
import { routers, otherRouter, appRouter } from './router';

Vue.use(VueRouter);

// 路由配置
const RouterConfig = {
  // mode: 'history',
  routes: routers
};

export const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
	Util.title(to.meta.title);
	let store = router.app.$options.store;
	let app = store.state.app;
  let userInfo = JSON.parse(localStorage.getItem('userInfo'));
	if (app.name === '' && to.name !== 'login') {
    if (userInfo === null) {
			router.push({name: 'login'});
			store.commit('logout', router.app);
			store.commit('clearOpenedSubmenu');
			return;
		} else {
      let curName = userInfo.username;
			store.commit('setUsername', curName);
		}
		
	}
  const curRouterObj = Util.getRouterObjByName(
    [otherRouter, ...appRouter],
    to.name
  );
  if (curRouterObj && curRouterObj.access !== undefined) {
    // 需要判断权限的路由
    let accessCode = userInfo.access;
    
    if (accessCode.indexOf(curRouterObj.access) !== -1) {
      Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next); // 如果在地址栏输入的是一级菜单则默认打开其第一个二级菜单的页面
    } else {
      next({
        replace: true,
        name: 'error-403'
      });
    }
  } else {
    // 没有配置权限的路由, 直接通过
    Util.toDefaultPage([...routers], to.name, router, next);
  }
});

router.afterEach(to => {
  Util.openNewPage(router.app, to.name, to.params, to.query);
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});
